﻿using Newtonsoft.Json.Linq;
using Sunny.UI;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Input;
using System.Xml.Linq;
using 可视化工具.Controller.Csv文件交互;
using static System.Net.Mime.MediaTypeNames;
using static System.Windows.Forms.AxHost;

namespace 可视化工具.View.Csv文件
{
    public partial class CsvCailperForm_ : Form
    {
        List<string> vlaueNames = new List<string>();   
        public CsvCailperForm_()
        {
            try
            {
                InitializeComponent();
                vlaueNames.Add(textBox卡尺.Text.Trim());
                vlaueNames.Add(textBox距离转换.Text.Trim());
                vlaueNames.Add(textBox结果分析.Text.Trim());
                vlaueNames.Add(textBox间距.Text.Trim());
            }
            catch
            {

            }
        }

        public CsvCailperForm_(CsvMainForm csvMainForm) : this()
        {
            this.csvMainForm = csvMainForm;
        }

        public string 卡尺Begin = string.Empty;
        private void btn卡尺_Click(object sender, EventArgs e)
        {
            try
            {
                卡尺Changer(true);
                卡尺Begin = textBox卡尺.Text.Trim();
            }
            catch
            {

            }
        }

        private  void btn卡尺1_Click(object sender, EventArgs e)
        {
            try
            {
                if(卡尺Begin== textBox卡尺.Text.Trim())
                {
                    卡尺Begin = string.Empty;
                    卡尺Changer(false);
                    return;
                }
                if (VlueNameChanged(textBox卡尺, 卡尺Begin, textBox卡尺.Text.Trim()))
                {
                    vlaueNames[0] = textBox卡尺.Text.Trim();
                    if (csvMainForm.csvConfig.cailpeVlues.ContainsKey(label1.Text + textBox模块工具.Text.Trim()))
                    {
                        //将配置文件的数据修改
                        List<string> strs= csvMainForm.csvConfig.cailpeVlues[label1.Text + textBox模块工具.Text.Trim()];
                        strs[0] = vlaueNames[0];
                        csvMainForm.controller.SerializeToIni(csvMainForm.csvConfig, csvMainForm.FullPath + "\\csv配置文件.json");
                        //实时修改csv文件

                        try
                        {
                            csvMainForm.controller.ReFlushColumns();
                        }
                        catch
                        {

                        }
                    }
                }
                else
                {
                    MessageBox.Show("名称不能重复", "错误");
                    textBox卡尺.Text = 卡尺Begin;
                }
                
            }
            catch
            {
                //MessageBox.Show("btn卡尺1_Click");
            }
            finally
            {
                卡尺Begin = string.Empty;
                卡尺Changer(false);
            }
        }
        public void 卡尺Changer(bool state)
        {
            try
            {
                btn卡尺.Enabled = !state;
                textBox卡尺.Enabled = state;
                btn卡尺1.Enabled = state;
                if (state)
                {
                    textBox卡尺.Focus();
                }
                else
                {
                    btn卡尺.Focus();
                }
            }
            catch
            {

            }
        }
        public string 距离转换Begin = string.Empty;
        private void btn距离转换_Click(object sender, EventArgs e)
        {
            try
            {
                距离转换Changer(true);
                距离转换Begin = textBox距离转换.Text.Trim();
            }
            catch
            {

            }
        }

        private  void btn距离转换1_Click(object sender, EventArgs e)
        {
            try
            {
                if(距离转换Begin== textBox距离转换.Text.Trim())
                {
                    距离转换Changer(false);
                    距离转换Begin = string.Empty;
                    return;
                }
                if (VlueNameChanged(textBox距离转换, 距离转换Begin, textBox距离转换.Text.Trim()))
                {
                    vlaueNames[1] = textBox距离转换.Text.Trim();
                    if (csvMainForm.csvConfig.cailpeVlues.ContainsKey(label1.Text + textBox模块工具.Text.Trim()))
                    {
                        List<string> strs = csvMainForm.csvConfig.cailpeVlues[label1.Text + textBox模块工具.Text.Trim()];
                        strs[1] = vlaueNames[1];
                        csvMainForm.controller.SerializeToIni(csvMainForm.csvConfig, csvMainForm.FullPath + "\\csv配置文件.json");
                        try
                        {
                            csvMainForm.controller.ReFlushColumns();
                        }
                        catch
                        {

                        }
                    }
                }
                else
                {
                    MessageBox.Show("名称不能重复", "错误");
                    textBox距离转换.Text = 距离转换Begin;

                }
            }
            catch
            {
               // MessageBox.Show("btn距离转换1_Click");
            }
            finally
            {
                距离转换Changer(false);
                距离转换Begin = string.Empty;
            }
           
        }
        public void 距离转换Changer(bool state)
        {
            try
            {
                btn距离转换.Enabled = !state;
                textBox距离转换.Enabled = state;
                btn距离转换1.Enabled = state;
                if (state)
                {
                    textBox距离转换.Focus();
                }
                else
                {
                    btn距离转换.Focus();
                }
            }
            catch
            {

            }
        }

        public string 结果分析Begin = string.Empty;
        private void btn结果分析_Click(object sender, EventArgs e)
        {
            try
            {
                结果分析Changer(true);
                结果分析Begin = textBox结果分析.Text.Trim();
            }
            catch
            {
                
            }
        }


        private  void btn结果分析1_Click(object sender, EventArgs e)
        {
            try
            {
                if(结果分析Begin== textBox结果分析.Text.Trim())
                {
                    结果分析Changer(false);
                    结果分析Begin = string.Empty;
                    return;
                }
                if (VlueNameChanged(textBox结果分析, 结果分析Begin, textBox结果分析.Text.Trim()))
                {
                    vlaueNames[2] = textBox结果分析.Text.Trim();
                    if (csvMainForm.csvConfig.cailpeVlues.ContainsKey(label1.Text + textBox模块工具.Text.Trim()))
                    {
                        List<string> strs = csvMainForm.csvConfig.cailpeVlues[label1.Text + textBox模块工具.Text.Trim()];
                        strs[2] = vlaueNames[2];
                        csvMainForm.controller.SerializeToIni(csvMainForm.csvConfig, csvMainForm.FullPath + "\\csv配置文件.json");
                        try
                        {
                            csvMainForm.controller.ReFlushColumns();
                        }
                        catch
                        {

                        }
                    }
                }
                else
                {
                    MessageBox.Show("名称不能重复", "错误");
                    textBox结果分析.Text = 结果分析Begin;
                }
              
            }
            catch
            {
              //  MessageBox.Show("btn结果分析1_Click");
            }
            finally
            {
                结果分析Changer(false);
                结果分析Begin = string.Empty;
            }
        }
        public void 结果分析Changer(bool state)
        {
            try
            {
                btn结果分析.Enabled = !state;
                textBox结果分析.Enabled = state;
                btn结果分析1.Enabled = state;
                if (state)
                {
                    textBox结果分析.Focus();
                }
                else
                {
                    btn结果分析.Focus();
                }
            }
            catch
            {

            }
        }

        public string 间距Begin = string.Empty;
        private void btn间距_Click(object sender, EventArgs e)
        {
            try
            {
                间距Changer(true);
                间距Begin = textBox间距.Text.Trim();
            }
            catch
            {
                 
            }
        }

        private  void btn间距1_Click(object sender, EventArgs e)
        {
            try
            {
                if(间距Begin== textBox间距.Text.Trim())
                {
                    间距Changer(false);
                    间距Begin = string.Empty;
                    return;
                }
                if (VlueNameChanged(textBox间距, 间距Begin, textBox间距.Text.Trim()))
                {
                    vlaueNames[3] = textBox间距.Text.Trim();
                    if (csvMainForm.csvConfig.cailpeVlues.ContainsKey(label1.Text + textBox模块工具.Text.Trim()))
                    {
                        List<string> strs = csvMainForm.csvConfig.cailpeVlues[label1.Text + textBox模块工具.Text.Trim()];
                        strs[3] = vlaueNames[3];
                        csvMainForm.controller.SerializeToIni(csvMainForm.csvConfig, csvMainForm.FullPath + "\\csv配置文件.json");

                        try
                        {
                            csvMainForm.controller.ReFlushColumns();
                        }
                        catch
                        {

                        }
                    }
                }
                else
                {
                    MessageBox.Show("名称不能重复", "错误");
                    textBox间距.Text = 间距Begin;
                }
                
            }
            catch
            {
              //  MessageBox.Show("btn间距1_Click");
            }
            finally
            {
                间距Changer(false);
                间距Begin = string.Empty;
            }
        }
        public void 间距Changer(bool state)
        {
            try
            {
                btn间距.Enabled = !state;
                textBox间距.Enabled = state;
                btn间距1.Enabled = state;
                if (state)
                {
                    textBox间距.Focus();
                }
                else
                {
                    btn间距.Focus();
                }
            }
            catch
            {

            }
        }
        public string 角度Begin = string.Empty;
        /// <summary>
        /// 
        /// </summary>
        public string beginText = string.Empty;
        private CsvMainForm csvMainForm;

        /// <summary>
        /// 模块工具重命名_更改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void btn模块工具_Click(object sender, EventArgs e)
        {
            try
            {
                btn模块工具.Enabled = false;
                btn模块工具1.Enabled = true;
                textBox模块工具.Enabled = true;
                beginText = textBox模块工具.Text.Trim();
                textBox模块工具.Focus();
            }
            catch
            {
               // MessageBox.Show("btn模块工具_Click");
            }
        }
        /// <summary>
        /// 模块工具重命名_确认
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public async void btn模块工具1_Click(object sender, EventArgs e)
        {
            try
            {
                if (textBox模块工具.Text.Trim() == beginText.Trim())
                {
                    beginText = string.Empty;
                    btn模块工具1.Enabled = false;
                    textBox模块工具.Enabled = false;
                    btn模块工具.Enabled = true;
                    btn模块工具.Focus();
                    return;
                }
                else
                {
                    //更改校验 不允许重名
                    if (!csvMainForm.CsvModelFormNameChanged(label1.Text + textBox模块工具.Text.Trim()))
                    {
                        MessageBox.Show("模块工具名称重复!");
                        textBox模块工具.Text = beginText.Trim();
                    }
                    else
                    {

                        if (textBox模块工具.Text.Trim() == "")
                        {
                            MessageBox.Show("名称不能为空", "错误");
                            textBox模块工具.Text = beginText.Trim();
                        }
                        else
                        {
                            //重命名
                            if (this.Parent is TabPage page)
                            {
                               
                                if (csvMainForm.csvConfig.cailpeVlues.ContainsKey(label1.Text+beginText))
                                {
                                    page.Text = label1.Text + textBox模块工具.Text.Trim();
                                    page.Name = label1.Text + textBox模块工具.Text.Trim();
                                    // 获取旧键对应的值
                                    var value = csvMainForm.csvConfig.cailpeVlues[label1.Text+beginText];
                                    // 获取字典的顺序
                                    var items = csvMainForm.csvConfig.cailpeVlues.ToList();
                                    // 删除旧的键
                                    csvMainForm.csvConfig.cailpeVlues.Remove(label1.Text + beginText);
                                    // 在原位置插入新键
                                    bool inserted = false;
                                    csvMainForm.csvConfig.cailpeVlues.Clear();
                                    foreach (var item in items)
                                    {
                                        if (!inserted && item.Key == label1.Text + beginText)
                                        {
                                            // 在找到的旧键位置插入新键
                                            csvMainForm.csvConfig.cailpeVlues.Add(page.Text, value);
                                            inserted = true;
                                        }
                                        else
                                        {
                                            // 保持其他项
                                            csvMainForm.csvConfig.cailpeVlues.Add(item.Key, item.Value);
                                        }
                                    }
                                    csvMainForm.controller.SerializeToIni(csvMainForm.csvConfig, csvMainForm.FullPath + "\\csv配置文件.json");
                                    //更改csv文件
                                    try
                                    {
                                        //重置所有列头节点
                                        csvMainForm.controller.ReFlushColumns();
                                    }
                                    catch
                                    {

                                    }
                                    // 获取旧键对应的值
                                    var ngValue = csvMainForm.hwindowFlowNodeUrseControl.ngDictionary[label1.Text + beginText];
                                    // 获取字典的顺序
                                    var ngDic = csvMainForm.hwindowFlowNodeUrseControl.ngDictionary.ToList();
                                    // 删除旧的键
                                    csvMainForm.csvConfig.cailpeVlues.Remove(label1.Text + beginText);
                                    // 在原位置插入新键
                                    bool inserted2= false;
                                    csvMainForm.hwindowFlowNodeUrseControl.ngDictionary.Clear();
                                    foreach (var item in ngDic)
                                    {
                                        if (!inserted2 && item.Key == label1.Text + beginText)
                                        {
                                            // 在找到的旧键位置插入新键
                                            csvMainForm.hwindowFlowNodeUrseControl.ngDictionary.Add(page.Text, ngValue);
                                            inserted2 = true;
                                        }
                                        else
                                        {
                                            // 保持其他项
                                            csvMainForm.hwindowFlowNodeUrseControl.ngDictionary.Add(item.Key, item.Value);
                                        }
                                    }
                                    csvMainForm.hwindowFlowNodeUrseControl.Refresh(true);
                                }
                            }
                        }
                    }
                }
            }
            catch(Exception ex)
            {
               textBox模块工具.Text = beginText.Trim();
                MessageBox.Show("重命名失败"+ex.Message);
            }
            finally
            {
                beginText = string.Empty;
                btn模块工具1.Enabled = false;
                textBox模块工具.Enabled = false;
                btn模块工具.Enabled = true;
                btn模块工具.Focus();

            }
        }
        public bool VlueNameChanged(UITextBox currentTextBox,string beginText, string text)
        {
            try
            {
                if (beginText == text)
                {
                    return true;
                }
                if (vlaueNames.Contains(text))
                {
                    return false;
                }
                else
                {
                    if (text.Trim() == "")
                    {
                        return false;
                    }else
                    {
                        return true;
                    }
                }
            }
            catch
            {
               // MessageBox.Show("VlueNameChanged");
                return false;
            }
        }
        /// <summary>
        /// 卡尺选中
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void checkBox卡尺_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                if (checkBox卡尺.Tag.ToString() == "1") return;

                bool state = checkBox卡尺.Checked;
                // 显示确认框
                DialogResult result = MessageBox.Show(
                    "请确保当前软件在非运行状态 您确定要执行此操作吗？",
                    "确认操作",
                    MessageBoxButtons.YesNo,
                    MessageBoxIcon.Question
                );
                if (result == DialogResult.Yes)
                {
                    csvMainForm.csvConfig.cailperValueState(label1.Text + textBox模块工具.Text.Trim(), 0, state);
                }
                else
                {
                    checkBox卡尺.Tag = 1;
                    checkBox卡尺.Checked = !state;
                    checkBox卡尺.Tag = 0;
                }
            }
            catch
            {

            }
        }
        /// <summary>
        /// 距离转换选中
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void checkBox距离转换_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                if (checkBox距离转换.Tag.ToString() == "1") return;
                bool state = checkBox距离转换.Checked;
                // 显示确认框
                DialogResult result = MessageBox.Show(
                    "请确保当前软件在非运行状态 您确定要执行此操作吗？",
                    "确认操作",
                    MessageBoxButtons.YesNo,
                    MessageBoxIcon.Question
                );
                if (result == DialogResult.Yes)
                {
                    //执行正确的操作
                    csvMainForm.csvConfig.cailperValueState(label1.Text + textBox模块工具.Text.Trim(), 1, state);
                }
                else
                {
                    checkBox距离转换.Tag = 1;
                    checkBox距离转换.Checked = !state;
                    checkBox距离转换.Tag = 0;
                }
            }
            catch
            {

            }
        }
        /// <summary>
        /// 结果分析选中
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void checkBox结果分析_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                if (checkBox结果分析.Tag.ToString() == "1") return;
                bool state = checkBox结果分析.Checked;
                // 显示确认框
                DialogResult result = MessageBox.Show(
                    "请确保当前软件在非运行状态 您确定要执行此操作吗？",
                    "确认操作",
                    MessageBoxButtons.YesNo,
                    MessageBoxIcon.Question
                );
                if (result == DialogResult.Yes)
                {
                    csvMainForm.csvConfig.cailperValueState(label1.Text + textBox模块工具.Text.Trim(), 2, state);
                }
                else
                {
                    checkBox结果分析.Tag = 1;
                    checkBox结果分析.Checked = !state;
                    checkBox结果分析.Tag = 0;
                }
            }
            catch
            {

            }
           
        }
        /// <summary>
        /// 间距选中
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void checkBox间距_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                if (checkBox间距.Tag.ToString() == "1") return;
                bool state = checkBox间距.Checked;
                // 显示确认框
                DialogResult result = MessageBox.Show(
                    "请确保当前软件在非运行状态 您确定要执行此操作吗？",
                    "确认操作",
                    MessageBoxButtons.YesNo,
                    MessageBoxIcon.Question
                );
                if (result == DialogResult.Yes)
                {
                    //执行正确的操作
                    csvMainForm.csvConfig.cailperValueState(label1.Text + textBox模块工具.Text.Trim(), 3, state);
                }
                else
                {
                    checkBox间距.Tag = 1;
                    checkBox间距.Checked = !state;
                    checkBox间距.Tag = 0;
                }
            }
            catch
            {

            }
        }
    }
}